﻿
#include "common.h"
#pragma comment(lib,"imm32.lib")

bool wstrless(WCHAR* s1,WCHAR* s2)
{
	return wcscmp(s1,s2)<0;
}

WCHAR* wstrdup(WCHAR* wstr)
{
	//无论如何都返回一个字符串
	if (wstr==NULL)
		wstr=L"";
	int len=wcslen(wstr);
	WCHAR* dup=new WCHAR[len+1];
	wcscpy(dup,wstr);
	return dup;
}

void wstrfree(WCHAR* wstr)
{
	if (wstr!=NULL)
		delete[] wstr;
}

BOOL wstreql(WCHAR* wstr1,WCHAR* wstr2)
{
	return wcscmp(wstr1,wstr2)==0;
}

BOOL fequal(float Value1,float Value2)
{
	if (fabs(Value1-Value2)>0.0000001)
		return FALSE;
	return TRUE;
}

int Mul1000(float Value)
{
	int Round;
	if (Value>=0)
		Round=(Value*10000+5)/10;
	else
		Round=(Value*10000-5)/10;
	return Round;
}

float fround(float Value,unsigned int NumDigits)
{
	if (Value>INT_MAX)
		return 999999999.0f;
	if (Value<INT_MIN)
		return -999999999.0f;
	if (NumDigits>9)
		return Value;
	int x=1;
	for (int i=0;i<NumDigits;i++)
		x=x*10;
	double Value2=Value*x;
	if (Value2>=0)
		Value2=(LONGLONG)(Value2+0.5f);
	else
		Value2=(LONGLONG)(Value2-0.5f);
	Value=(float)(Value2/x);
	return Value;
}

void FormatInt64WithChineseStyle(WCHAR* Buf,LONGLONG Value,BOOL ForceSign)
{
	int Sign=1;
	if (Value<0)
	{
		Value=-Value;
		Sign=-1;
	}
	int Len=0;
	if (Value==0)
	{
		Buf[0]='0';
		Len=1;
	}
	else
	{
		int n=0;
		while (Value>0)
		{
			Buf[Len]=Value%10+'0';
			Value=Value/10;
			Len++;
			n++;
			if (n%4==0)
			{
				Buf[Len]=' ';
				Len++;
			}
		}
		if (n%4==0)
			Len--;
	}
	if (Sign==-1)
	{
		Buf[Len]='-';
		Len++;
	}
	else if (ForceSign)
	{
		Buf[Len]='+';
		Len++;
	}
	for (int i=0;i<Len/2;i++)
	{
		WCHAR t=Buf[i];
		Buf[i]=Buf[Len-1-i];
		Buf[Len-1-i]=t;
	}
	Buf[Len]=0;
}

int GenerateId()
{
	//42亿个肯定够用
	static int GlobalId=0;
	return GlobalId++;
}

BOOL PointInRect(int X,int Y,int Left,int Top,int Width,int Height)
{
	if (X>Left && X<=Left+Width && Y>Top && Y<=Top+Height)
	{
		return TRUE;
	}
	return FALSE;
}

void SetImmBoxPosition(HWND hWnd,int X,int Y)
{
	HIMC hImc=ImmGetContext(hWnd);
	COMPOSITIONFORM CompForm;
	CompForm.dwStyle=CFS_POINT;
	CompForm.ptCurrentPos.x=X;
	CompForm.ptCurrentPos.y=Y;
	ImmSetCompositionWindow(hImc,&CompForm);
	ImmReleaseContext(hWnd,hImc);
}

int GetImmResultString(HWND hWnd,WCHAR* ImmBuf,int BufLen)
{
	HIMC hImc=ImmGetContext(hWnd);
	DWORD Len=ImmGetCompositionString(hImc,GCS_RESULTSTR,NULL,0);
	//IMM_ERROR_NODATA或IMM_ERROR_GENERAL，以及输入过多都返回空字符串
	//调用ImmReleaseContext后会自动清空没取走的数据
	if (Len<0)
	{
		Len=0;
		ImmBuf[0]=0;
	}
	else
	{
		//返回结果不含\0，按缓冲区大小截断
		Len=min(Len,(BufLen-1)*sizeof(WCHAR));
		ImmGetCompositionString(hImc,GCS_RESULTSTR,ImmBuf,Len);
		Len=Len/sizeof(WCHAR);
		ImmBuf[Len]=0;
	}
	ImmReleaseContext(hWnd,hImc);
	return Len;
}

void CopyTextToClipboard(HWND hWnd,WCHAR* Text,int Len)
{
	if (OpenClipboard(hWnd))
	{
		EmptyClipboard();
		int Size=(Len+1)*sizeof(WCHAR);
		HGLOBAL hMem=GlobalAlloc(GMEM_MOVEABLE,Size);
		if (hMem!=NULL)
		{
			WCHAR* Ptr=(WCHAR*)GlobalLock(hMem);
			memcpy(Ptr,Text,Size);
			Ptr[Len]=0;
			GlobalUnlock(hMem);
			SetClipboardData(CF_UNICODETEXT,hMem);
		}
		CloseClipboard();
	}
}

void PasteTextFromClipboard(HWND hWnd,WCHAR* Buffer,int MaxBufLen)
{
	if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) 
		return; 
	if (OpenClipboard(hWnd))
	{
		HANDLE hMem=GetClipboardData(CF_UNICODETEXT); 
		if (hMem!=NULL)
		{
			WCHAR* Text=(WCHAR*)GlobalLock(hMem);
			if (Text!=NULL)
			{
				for (int i=0;i<MaxBufLen;i++)
				{
					Buffer[i]=Text[i];
					if (Text[i]==0)
						break;
				}
				Buffer[MaxBufLen-1]=0;
				GlobalUnlock(hMem);
			}
		}
		CloseClipboard();
	}
}

int ParseIntText(WCHAR* Text,int Len)
{
	if (Len==-1)
		Len=wcslen(Text);

	int Sign=1;
	int Cur=0;
	while (Cur<Len)
	{
		WCHAR ch=Text[Cur];
		if (ch=='+')
		{
			Sign=1;
			Cur++;
			break;
		}
		else if (ch=='-')
		{
			Sign=-1;
			Cur++;
			break;
		}
		else if (ch>='0' && ch<='9')
		{
			break;
		}
		Cur++;
	}

	int Value=0;
	while (Cur<Len)
	{
		WCHAR ch=Text[Cur];
		if (ch>='0' && ch<='9')
		{
			//溢出检查
			if (Sign==1)
			{
				if (Value>=214748364 && (ch-'0')>=8)
					return INT_MAX;
			}
			else
			{
				if (Value>=214748364 && (ch-'0')>=9)
					return INT_MIN;
			}
			Value=Value*10+(ch-'0');
		}
		else if (ch=='.')
		{
			//跳过所有非数字字符，仅解析文本中存在的数字，但是遇到小数点立即截断
			break;
		}
		Cur++;
	}
	return Value*Sign;
}

float ParseFloatText(WCHAR* Text,int Len)
{
	if (Len==-1)
		Len=wcslen(Text);

	enum _State
	{
		SSign,
		SInteger,
		SDecimal,
	};
	float Scale[12]={0.1f,0.01f,0.001f,0.0001f,0.00001f,0.000001f,0.0000001f,
		0.00000001f,0.000000001f,0.0000000001f,0.00000000001f,0.000000000001f};

	int Sign=1;
	float Value=0.0f;
	int IntCount=0;
	int DecimalCount=0;
	_State State=SSign;

	int Cur=0;
	while (Cur<Len)
	{
		WCHAR ch=Text[Cur];
		if (State==SSign)
		{
			if (ch=='+')
			{
				Sign=1;
				State=SInteger;
			}
			else if (ch=='-')
			{
				Sign=-1;
				State=SInteger;
			}
			else if (ch>='0' && ch<='9')
			{
				State=SInteger;
				continue;
			}
			else if (ch=='.')
			{
				//允许省略整数部分的0
				State=SDecimal;
			}
			else if (ch=='%')
			{
				return 0.0f;
			}
		}
		else if (State==SInteger)
		{
			if (ch>='0' && ch<='9')
			{
				Value=Value*10.0f+(ch-'0');
				IntCount++;	
				if (IntCount==12)
					return Value*Sign;
			}
			else if (ch=='.')
			{
				//允许省略整数部分的0
				State=SDecimal;
			}
			else if (ch=='%')
			{
				return Value*Sign*0.01f;
			}
		}
		else if (State==SDecimal)
		{
			if (ch>='0' && ch<='9')
			{
				Value=Value+(ch-'0')*Scale[DecimalCount];
				DecimalCount++;
				//float可以支持更大范围，但是可能会损失精度，没必要支持更多位
				if (IntCount+DecimalCount==12)
					return Value*Sign;
			}
			else if (ch=='%')
			{
				return Value*Sign*0.01f;
			}
		}
		Cur++;
	}
	return Value*Sign;
}

